<?php
/*
  ===========================================================================
  index.php - HollandscheMeesters

  naam: Mamadou Diallo
  Author   : LimeLight B.V., 2012, http://www.thelimelight.nl/
  ===========================================================================
 */

// LOGIN MODULE
ob_start();
error_reporting(E_ALL);
include_once('controller/includes/constants.php');
include_once('controller/includes/db_access.php');


if (isset($_POST['actType']) && ($_POST['actType'] == 'check')) {
    // start een sessie of zet een sessie voort


    if (trim($_POST['myusername']) != "" && trim($_POST['mypassword']) != "") {


      $myusername = $_POST['myusername'];
       $mypassword = $_POST['mypassword'];
        $myusername = stripslashes($myusername);
        $mypassword = stripslashes($mypassword);
        $myusername = mysql_real_escape_string($myusername);
        $mypassword = mysql_real_escape_string($mypassword);
        $encrypted_mypassword = md5($mypassword);

//$query ="SELECT * FROM adm_acs WHERE username='$myusername' AND password='$encrypted_mypassword'";
//$result= mysql_query($query);
//$count = mysql_num_rows($result);

        
$q = new Query;
$q
    ->select(
        array(
            '`adm_acs`.`username`',
            '`adm_acs`.`password`',
        )
    )
    ->from('`adm_acs`')
    ->where_equal_to(
        array(
            '`adm_acs`.`username`' => $myusername,
            '`adm_acs`.`password`' => $encrypted_mypassword
        )
    )
    ->limit(1);
    $q->run();
    
$count = $q->get_selected_count();

die($count);

if($count == 1){

session_register("myusername");
session_register("mypassword");

            $timeout = 60 * 60; // Number of seconds until it times out.
// Check if the timeout field exists.
            if (isset($_SESSION['timeout'])) {
                // See if the number of seconds since the last
                // visit is larger than the timeout period.
                $duration = time() - (int) $_SESSION['timeout'];
                if ($duration > $timeout) {
                    // Destroy the session and restart it.
                    session_destroy();
                    session_start();
                }
            }

// Update the timout field with the current time.
            $_SESSION['timeout'] = time();




            //$lbValues['dt'] = date('Y-m-d H:i:s');
            //$lbValues['user'] = mysql_result($result, 0, "username");
            //$lbValues['ip'] = $_SERVER['REMOTE_ADDR'];
            //$log_id = $db->Insert('logs', $lbValues, '', true);
            

            header("location: views/product_list.php");
        } else {
            echo "Je inloggegevens zijn niet correct. 
                              Controleer je gegevens en probeer het nogmaals.<br /><br />";
        }

        ob_end_flush();
    }
}
?>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login db me</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function()
{
$("#username").focus();
$(".user").keyup(function()
{
var email=$(this).val();
var dataString = 'email='+ email ;
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

if(ck_email.test(email))
{
$.ajax({
type: "POST",
url: "avatar.php",
data: dataString,
cache: false,

success: function(html)
{

$("#img_box").html("<img src='http://www.gravatar.com/avatar.php?gravatar_id="+html+"?d=mm' class='avatar_img' />");
}
});
}


});

});
</script>
<style>
body
{
background-color:#fff;
}
#login_box .input
{
background-color: #FFFFFF;
    border: 1px solid #CCCCCC;
    border-radius: 3px 3px 3px 3px;
    color: #555555;
    display: inline-block;
    font-size: 13px;
    height: 18px;
    line-height: 18px;
    margin-bottom: 9px;
    padding: 4px 4px 4px 23px;
    width: 210px;
}
.btn
{
-moz-border-bottom-colors: none;
    -moz-border-image: none;
    -moz-border-left-colors: none;
    -moz-border-right-colors: none;
    -moz-border-top-colors: none;
  background-color: #DA4F49;
    background-image: -moz-linear-gradient(center top , #EE5F5B, #BD362F);
    background-repeat: repeat-x;
    border-color: #E6E6E6 #E6E6E6 #B3B3B3;
    border-radius: 4px 4px 4px 4px;
    border-style: solid;
    border-width: 1px;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2) inset, 0 1px 2px rgba(0, 0, 0, 0.05);
    color: #fff;
    cursor: pointer;
    display: inline-block;
    font-size: 13px;
    line-height: 18px;
    margin-bottom: 0;
    padding: 4px 10px;
    text-align: center;
    text-shadow: 0 1px 1px rgba(255, 255, 255, 0.75);
    vertical-align: middle;
    float:right;
}
#login_box
{
padding:60px 30px 30px 30px;
border:solid 1px #dedede;
width:238px;
background-color:#fcfcfc;
margin-top:70px;

}
#img_box
{
   background-color: #FFFFFF;
    border: 1px solid #DEDEDE;
   
   
    margin-left: 77px;
    margin-top: -108px;
    position: absolute;
    width: 86px;
 height: 86px;
}
.user
{
background:url(icons/user.png) 4px no-repeat ;
}
.passcode
{
background:url(icons/lock.png) 4px no-repeat;
}
.avatar_img
{
padding:3px;
}
#login_container
{
background:url(blue.jpg) #006699;
    overflow: auto;
    width: 300px;

}
</style>
</head>
<body>
	
<div style='margin:0px auto;width:400px; text-align:center'>
<div id='login_container'>
<div id='login_box'>
<div id='img_box'><img src='http://www.gravatar.com/avatar/?d=mm' class='avatar_img'/></div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="sform" id="sform" onSubmit="return checkForm(this)">
<input type="hidden" name="actType" value="check" />
<input type='text' id='username' name="myusername" class='input user'/> <br/>
<input type='password' id='password' name="mypassword" class='input passcode'/> <br/>
<input type='submit' value=' Login ' class='btn' />
<div style='clear:both'></div>
</form>
</div>
</div>

</div>
</body>
</html>
            </form>

        </div>


    </body>
</html>